package com.ellfors.common.ext

import android.content.Context
import android.widget.Toast
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import com.ellfors.common.app.MyApp
import com.ellfors.common.utils.DensityUtil

fun Context.color(
    @ColorRes
    id: Int
) = ContextCompat.getColor(this, id)

fun color(
    @ColorRes
    id: Int
) = ContextCompat.getColor(MyApp.context, id)

fun Context.string(
    @StringRes
    id: Int
) = this.getString(id)

fun string(
    @StringRes
    id: Int
) = MyApp.context.getString(id)

fun Context.drawable(
    @DrawableRes
    id: Int
) = ContextCompat.getDrawable(this, id)

fun drawable(
    @DrawableRes
    id: Int
) = ContextCompat.getDrawable(MyApp.context, id)

fun Context.colorStateList(
    @ColorRes
    id: Int
) = ContextCompat.getColorStateList(this, id)

fun colorStateList(
    @ColorRes
    id: Int
) = ContextCompat.getColorStateList(MyApp.context, id)

fun String?.toast(duration: Int = Toast.LENGTH_SHORT) {
    if (this.isNullOrBlank()) {
        return
    }
    Toast.makeText(MyApp.context, this, duration).show()
}

fun Int.dp2px(): Int = DensityUtil.dp2px(this)

fun Int.sp2px(): Int = DensityUtil.sp2px(this)